<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

abstract class aDB {

    /**
     * 􁬳􀴳􀷄􀴝􀬪,􀕗􁯈􁗝􀷈􀕯􁧛􀝐􁯈􁗝􀗞􀯳
     */
    abstract public function conn();

    /**
     * 􀝎􁭆query􀺱􁧃
     * @param string $sql sql􁧍􀝙
     * @return mixed
     */
    abstract public function query($sql);

    /**
     * 􀺱􁧃􀥚􁤈􀷄􀴝
     * @param string $sql sql􁧍􀝙
     * @return array
     */
    abstract public function getAll($sql);

    /**
     * 􀜔􁤈􀷄􀴝
     * @param string $sql sql􁧍􀝙
     * @return array
     */
    abstract public function getRow($sql);

    /**
     * 􀺱􁧃􀜔􀓻􀷄􀴝 􀦇 count(*)
     * @param string $sql sql􁧍􀝙
     * @return mixed
     */
    abstract public function getOne($sql);

    /**
     * 􁛔􀛖􀚠􀭌sql􀬚􀲗􁤈
     * @param array $data 􀙉􁘶􀷄􁕟 􁲫/􀘀􀓨􁤒􁌱􀚜/􀘀􀩒􀬫
     * @param string $table 􁤒􀝷􀨁
     * @param string $act 􀛖􀖢/update/insert
     * @param string $where 􀹵􀕯,􁊠􀔭update
     * @return int 􀷛􀵊􀙁􁌱􁤈􁌱􀔆􁲫􀘀􀱲􀭽􀟥􁤈􀷄
     */
    abstract public function Exec($data, $table, $act = 'insert', $where = '0');

    /**
     * 􁬬􀢧􀓤􀓞􀹵insert􁧍􀝙􀔾􁊞􁌱􀔆􁲫􀘀
     */
    abstract public function lastId();

    /**
     * 􁬬􀢧􀓤􀓞􀹵􁧍􀝙􀭽􀟥􁌱􁤈􀷄
     */
    abstract public function affectRows();
}

class Mysql extends aDB {

    public $link = null;

    /**
     * 构造方法，连接数据库；􁯈􁗝􀗞􀯳
     */
    public function __construct() {
        $this->conn();
    }

    /**
     * 连接数据库，从配置文件读取信息；􁯈􁗝􀗞􀯳
     */
    public function conn() {
        $cfg = include "./config.php";
        $this->link = new mysqli($cfg["host"], $cfg["user"], $cfg["pwd"], $cfg["db"]);
        $this->query("set names " . $cfg["charset"]);
        //print_r($mysqli);
    }

    public function query($sql) {
        //mysqli_query();
        return $this->link->query($sql);
    }

    public function getAll($sql) {
        //mysqli_fetch_assoc():
        $res = $this->query($sql);
        $arr = array(); //不定义的话，若查询结果为空，会报错：未定义$arr;
        while ($row = $res->fetch_assoc()) {
            $arr[] = $row;
        }
        return $arr;
        //print_r($arr);
    }

    public function getRow($sql) {
        //mysqli_fetch_assoc():
        $res = $this->query($sql);
        $row = array(); //不定义的话，若查询结果为空，会报错：未定义$arr;
        $row = $res->fetch_assoc();
        return $row;
        //print_r($arr);
    }

    public function getOne($sql) {
        //mysqli_fethc_row()
        $res = $this->query($sql);
        $one = $res->fetch_row()[0];
        return $one;
    }

    public function Exec($data, $table, $act = 'insert', $where = '0') {
        if ($act == "insert") {
            $sql = "insert into $table";
            $sql .= " (" . implode(",", array_keys($data)) . ") value";
            $sql .= " ('" . implode("','", array_values($data)) . "')";
        } else if ($act == "update") {
            $sql = "update $table set ";
            foreach ($data as $k => $v) {
                $sql .= $k . "='" . $v . "', ";
            }
            $sql = rtrim($sql, ", ") . " where " . $where;
        }
         // print_r($sql);
         // return false;
				return $this->query($sql);
    }

    public function lastId() {
        return $this->link->insert_id;
    }

    public function affectRows() {
        return $this->link->affected_rows;
    }

}

//$mysql = new Mysql;
//$mysql->conn();
//$a = $mysql->getone("select count(*) from comment where 1");
//print_r($a);
//var_dump($a);
//$arr = array("title" => "大堆xxx");
//$mysql->Exec($arr, "art","update","content='大堆'");
//var_dump($mysql->affectRows());
//print_r($mysql->lastId());

